﻿using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmFunctionEdit : FrmBase
    {
        public FrmFunctionEdit()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            bool flag = false;
            string paramValue = "N";
            if (this.ckUsedFlag.Checked)
            {
                paramValue = "Y";
            }
            string text = this.txtFunctionCode.Text;
            string str3 = this.txtFunctionName.Text;
            string str4 = this.txtObjectName.Text;
            string str5 = this.txtParameter.Text;
            string str6 = this.meDescribe.Text;
            string[] commandtexts = new string[3];
            DataParamCol[] parms = new DataParamCol[3];
            if (this.isNew)
            {
                commandtexts[0] = "INSERT INTO MXHLY_SYS_FUNCTION (FUNCTION_CODE ,FUNCTION_NAME ,OBJECT_NAME ,PARAMETER ,MODULE_CODE ,DESCRIBE ,USED_FLAG) \r\n   VALUES (:function_code ,:function_name ,:object_name ,:parameter ,:module_code ,:describe ,:used_flag)";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("function_code", text), new InputDataParam("function_name", str3), new InputDataParam("object_name", str4), new InputDataParam("parameter", str5), new InputDataParam("module_code", this.functionNode.GetValue("CODE").ToString()), new InputDataParam("describe", str6), new InputDataParam("used_flag", paramValue) });
            }
            else
            {
                commandtexts[0] = "UPDATE MXHLY_SYS_FUNCTION SET FUNCTION_CODE = :function_code ,FUNCTION_NAME = :function_name ,OBJECT_NAME = :object_name , PARAMETER = :parameter ,DESCRIBE = :describe ,USED_FLAG = :used_flag WHERE FUNCTION_CODE = :old_function_code";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("function_code", text), new InputDataParam("function_name", str3), new InputDataParam("object_name", str4), new InputDataParam("parameter", str5), new InputDataParam("describe", str6), new InputDataParam("used_flag", paramValue), new InputDataParam("old_function_code", this.functionNode.GetValue("CODE").ToString()) });
            }
            if (!this.isNew && (this.txtFunctionCode.Text != this.functionNode.GetValue("CODE").ToString()))
            {
                commandtexts[1] = "UPDATE MXHLY_SYS_FUNCTION_ROLE SET FUNCTION_CODE = :new_code WHERE FUNCTION_CODE = :old_code";
                parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("new_code", text), new InputDataParam("old_code", this.functionNode.GetValue("CODE").ToString()) });
                commandtexts[2] = "UPDATE MXHLY_SYS_MENU SET FUNCTION_CODE = :new_code WHERE FUNCTION_CODE = :old_code";
                parms[2] = parms[1];
                flag = SystemInfo.DbData.RunSql(commandtexts, parms) == 0;
            }
            else if (SystemInfo.DbData.ExecuteSql(commandtexts[0], parms[0]) >= 0)
            {
                flag = true;
            }
            if (!flag)
            {
                XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                if (!this.isNew)
                {
                    this.functionNode.SetValue("CODE", text);
                    this.functionNode.SetValue("NAME", str3);
                    this.functionNode.SetValue("OBJECT_NAME", str4);
                    this.functionNode.SetValue("PARAMETER", str5);
                    this.functionNode.SetValue("USED_FLAG", paramValue);
                    this.functionNode.SetValue("DESCRIBE", str6);
                }
                else
                {
                    TreeListNode node = this.functionNode.TreeList.AppendNode(new object[] { text, str3, str4, str5, paramValue, str6 }, this.functionNode);
                    node.ImageIndex = 2;
                    node.SelectImageIndex = 2;
                    this.functionNode.TreeList.FocusedNode = node;
                }
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        private void CreateFunctionList(string filename)
        {
            try
            {
                DataTable table = new DataTable();
                table.Columns.Add("FUNCTION_CODE");
                table.Columns.Add("FUNCTION_NAME");
                table.Columns.Add("OBJECT_NAME");
                Type[] types = Assembly.LoadFrom(SystemInfo.RunPath + filename + ".DLL").GetTypes();
                this.gvFunction.GridControl.DataSource = table;
                foreach (Type type in types)
                {
                    if (type.IsClass)
                    {
                        bool flag = false;
                        for (Type type2 = type.BaseType; type2 != null; type2 = type2.BaseType)
                        {
                            if (type2.Name == "FrmFunctionBase")
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag)
                        {
                            string text = "";
                            string formCode = "";
                            try
                            {
                                FrmFunctionBase base2 = (FrmFunctionBase)Assembly.LoadFrom(type.Module.ToString()).CreateInstance(type.FullName);
                                text = base2.Text;
                                if (!string.IsNullOrEmpty(base2.FormCode))
                                {
                                    formCode = base2.FormCode;
                                }
                            }
                            catch (Exception exception)
                            {
                                MessageBox.Show("获取窗口：" + type.Name + " 出错！\r\n" + exception.Message, "提示");
                            }
                            DataRow row = table.NewRow();
                            row[0] = formCode;
                            row[1] = text;
                            row[2] = type.Name;
                            table.Rows.Add(row);
                        }
                    }
                }
            }
            catch (Exception exception2)
            {
                MessageBox.Show("出错：" + exception2.Message, "提示");
            }
        }

        private void FrmFunctionEdit_Shown(object sender, EventArgs e)
        {
            this.txtFunctionCode.Focus();
        }

        private void gvFunction_DoubleClick(object sender, EventArgs e)
        {
            if (this.gvFunction.FocusedRowHandle >= 0)
            {
                this.txtFunctionCode.Text = this.gvFunction.GetFocusedRowCellValue("FUNCTION_CODE").ToString();
                this.txtFunctionName.Text = this.gvFunction.GetFocusedRowCellValue("FUNCTION_NAME").ToString();
                this.txtObjectName.Text = this.gvFunction.GetFocusedRowCellValue("OBJECT_NAME").ToString();
            }
        }

        public bool SetParameter(TreeListNode node, bool isnew)
        {
            if (!this.Init())
            {
                return false;
            }
            this.functionNode = node;
            this.isNew = isnew;
            if (!isnew)
            {
                this.txtFunctionCode.Text = node.GetValue("CODE").ToString();
                this.txtFunctionName.Text = node.GetValue("NAME").ToString();
                this.txtObjectName.Text = node.GetValue("OBJECT_NAME").ToString();
                this.txtParameter.Text = node.GetValue("PARAMETER").ToString();
                this.ckUsedFlag.Checked = node.GetValue("USED_FLAG").ToString() == "Y";
                this.meDescribe.Text = node.GetValue("DESCRIBE").ToString();
                this.Text = "功能修改";
                this.gvFunction.GridControl.Visible = false;
                base.Width = this.ucPanel1.Width + 4;
            }
            else
            {
                this.Text = "功能新增";
                this.txtFunctionCode.Text = node.GetValue("CODE") + "-";
                string filename = node.GetValue("OBJECT_NAME").ToString();
                this.CreateFunctionList(filename);
            }
            return true;
        }
    }
}
